﻿/**********************************************
 * 功能名称：显示用户所做的问卷答案
 *列表显示用户问卷答案.
 * 
 * 
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Wenjuan
{
    public partial class CustomerAnswer : System.Web.UI.Page
    {
        CustomDataContext db = new CustomDataContext();
        static DateTime begintime =  Convert.ToDateTime("2000-1-1 0:0:0");
        static DateTime endtime = Convert.ToDateTime("2020-1-1 0:0:0");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //查询当前操作员的店铺权限及客户回访等级权限。
                string MyUserId = HttpContext.Current.Session[SystemVars.sysSessionUserId].ToString();
                var RightList = from a in db.TCsm_JS_User
                                where a.UserID == MyUserId
                                select new
                                {
                                    a.ShopRightID,
                                    a.KefuRightID

                                };
                if (RightList.Count() > 0)
                {

                    string MyShopRightIdList = RightList.Single().ShopRightID;
                    if (!string.IsNullOrEmpty(MyShopRightIdList))
                    {
                        var DropList_ShopRight = MyShopRightIdList.Split(',');
                        foreach (var MyItems in DropList_ShopRight)
                        {
                            var MyShopName = (from a in db.TCrm_JC_Parameter
                                              where a.ID == Convert.ToInt32(MyItems)
                                              select a).Single();
                            ListItem InsertItem = new ListItem(MyShopName.ShopName, MyShopName.ShopID);
                            //填充店铺列表
                            DropDownList_ShopId.Items.Add(InsertItem);

                        }

                    }

                    string MyKefuRightIdList = RightList.Single().KefuRightID;
                    if (!string.IsNullOrEmpty(MyKefuRightIdList))
                    {
                        var DropList_KefuRight = MyKefuRightIdList.Split(',');
                        foreach (var MyItems in DropList_KefuRight)
                        {
                            var MyKefuRightName = (from a in db.TCrm_Sys_Items
                                                   where a.ItemID == MyItems
                                                   select a).Single();
                           
                                ListItem InsertItem = new ListItem(MyKefuRightName.ItemName, MyKefuRightName.ItemID);
                                //填充客户等级列表。
                                DropDownList_CustomerLevel.Items.Add(InsertItem);
                           

                        }

                    }

                }

                BindMyData(0);
                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
                EditFlag.Value = "add";

            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex)
        {
            int skipAccount = PageIndex * 10;
            string begin = TextBox_BeginTime.Text;
            string end = TextBox_EndTime.Text;
            string ShopId = DropDownList_ShopId.SelectedValue;
            string CustomerLevel = DropDownList_CustomerLevel.SelectedValue;
            string CustomerName = TextBox_CustomerName.Text;
            string TbId = TextBox_TbId.Text;
            

            if (begin != "" && end != "")
            {
                 begintime= Convert.ToDateTime(begin);
                 endtime = Convert.ToDateTime(end);
            }
                var MyQuery2 = (from a in db.TCrm_WJ_CustomerAnswer 
                                join b in db.TCrm_KF_Customer on a.CustomerID equals b.CustomerID
                               where 
                           
                                a.AnswerTime>begintime 
                               && a.AnswerTime<endtime
                               && b.CustomerLevel == CustomerLevel
                               && b.ShopID == ShopId
                               && b.TbID.Contains(TbId)
                               && b.CustomerName.StartsWith(CustomerName)
                               group a by new { a.StatID, a.ShopID, a.AnswerTime, a.CustomerID } into g
                               select new
                               {
                                   AnswerTime = g.Key.AnswerTime,
                                   CustomerID = g.Key.CustomerID,
                                   ShopID = g.Key.ShopID,
                                   StatID = g.Key.StatID,
                                   MyStatName  = (from h in db.TCrm_WJ_Statistics where h.StatID == g.Key.StatID select h.StatName).Single(),
                                   MyCustomerName = (from h in db.TCrm_KF_Customer where h.CustomerID == g.Key.CustomerID select h.CustomerName).Single(),
                                    MyTbId = (from h in db.TCrm_KF_Customer where h.CustomerID == g.Key.CustomerID select h.TbID).Single()
                                  
                               }).Skip(skipAccount).Take(10);
                GridView_Statics.DataSource = MyQuery2;
                GridView_Statics.DataBind();
            

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            string begin = TextBox_BeginTime.Text;
            string end = TextBox_EndTime.Text;
            string ShopId = DropDownList_ShopId.SelectedValue;
            string CustomerLevel = DropDownList_CustomerLevel.SelectedValue;
            string CustomerName = TextBox_CustomerName.Text;
            string TbId = TextBox_TbId.Text;


            if (begin != "" && end != "")
            {
                begintime = Convert.ToDateTime(begin);
                endtime = Convert.ToDateTime(end);
            }

            MyQuery = (from a in db.TCrm_WJ_CustomerAnswer
                       join b in db.TCrm_KF_Customer on a.CustomerID equals b.CustomerID
                       where a.AnswerTime > begintime
                       && a.AnswerTime < endtime
                       && b.CustomerLevel == CustomerLevel
                       && b.ShopID == ShopId
                       && b.TbID.Contains(TbId)
                       && b.CustomerName.StartsWith(CustomerName)
                       select a.StatID).Distinct().Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {

            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }

        #endregion
 
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            result.Visible = false;
        }
        protected void Button_Detail_Command(object sender, CommandEventArgs e)
        {
            string Statid = e.CommandArgument.ToString();
            Button ib = (Button)sender;
            GridViewRow gvr = (GridViewRow)ib.Parent.Parent;
            string CustomID = gvr.Cells[2].Text.ToString();
            MakeResult(Statid, CustomID);
        }

        //显示问卷细节
        private void MakeResult(string statid,string customid)
        {
            var query = (from a in db.TCrm_WJ_Statistics_Detail where a.StatID == statid select a).Single();
            string[] Questionids = query.QuestionID.ToString().Split(',');
            string inserdiv = "";
            string StatName=(from a in db.TCrm_WJ_Statistics where a.StatID==statid select a).Single().StatName;
            inserdiv += "<div><font size = 5 color = black face = '宋体'><center>" + StatName + "</center></font></div></br>";
            inserdiv +="<hr class = \"hrcss_double\" />";
            int i = 1;
            foreach (string Qid in Questionids)
            {
                string Title = (from a in db.TCrm_WJ_QuestionBase where a.QuestionID == Qid select a).Single().Title;
                inserdiv += "<p><font size = 3 color = red  face = '宋体'>" + i.ToString() + " " + Title + "</font></p>";
                var queryoptions = from a in db.TCrm_WJ_CustomerAnswer where a.CustomerID == customid && a.StatID == statid && a.QuestionID == Qid select a;
                string[] options = queryoptions.Single().OptionID.ToString().Split('/');
                string txt = queryoptions.Single().Txt.ToString();
                int count = options.Count();
                for (int k = 0; k < count;k++ )
                {
                    string optionlabel=(from a in db.TCrm_WJ_QuestionBase_Detail where  a.OptionID==options[k] && a.QuestionID==Qid select a.OptionLabel).Single().ToString();
                    inserdiv += "<p><font size = 2 color = blue  face = '宋体'>" + "("+options[k] + "):," + optionlabel + "</font></p>";
                }
                if (txt.Trim()!=null ||txt.Trim()!="")
                {
                    inserdiv += "<p><font size = 2 color = blue  face = '宋体'>Notes:" + txt + "</font></p>";
                }
                i++;
                inserdiv += "<hr class = \"hrcss_dashed\" />";


            }
            inserdiv += "<hr class = \"hrcss_double\" />";
            htmldiv.InnerHtml = inserdiv;
            result.Visible = true;
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('查看问卷明细','result');</script>", false); 
        }

        


    }
}
